/**
 * 作者:hanbo
 * 日期:2018/12/711:08
 * 描述:颜色类
 */

export class Color {
    /**
     * 颜色类构造器
     * @param color {String|Number[]|Object} 传输颜色支持16进制字符串、rgb数组，对象
     */
    constructor(color=[0,0,0]) {
        if (typeof(color) === "string") {
            let s = color.toLowerCase();
            //十六进制颜色值的正则表达式
            const reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
            if (!reg.test(color)) {
                console.error("非法颜色值");
                return
            }
            let r,g,b;
            if (color.length === 4) {//4位颜色处理,简写模式
                r = parseInt(color.substring(1, 2)+color.substring(1, 2), 16);
                g = parseInt(color.substring(2, 3)+color.substring(2, 3), 16);
                b = parseInt(color.substring(3)+color.substring(3), 16)
            } else {//7位颜色字符串处理
                 r = parseInt(color.substring(1, 3), 16);
                 g = parseInt(color.substring(3, 5), 16);
                 b = parseInt(color.substring(5), 16)
            }
            this.r = r / 255;
            this.g = g / 255;
            this.b = b / 255;


        } else if (Array.isArray(color)) {
            this.r = color[0] / 255;
            this.g = color[1] / 255;
            this.b = color[2] / 255;
        } else {
            this.r = color.r / 255;
            this.g = color.g / 255;
            this.b = color.b / 255;
        }
    }


    static randomColor(){
        let r=Math.ceil(Math.random()*255);
        let g=Math.ceil(Math.random()*255);
        let b=Math.ceil(Math.random()*255);
        return new Color([r,g,b]);
    }

}